package assess

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/assess/request"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type ImageAssessApi struct{}

// Image2Points 图像评估接口
// @Tags     Assess
// @Summary  图像评估 - 调用第三方API进行图像分析
// @Security ApiKeyAuth
// @accept   application/json
// @Produce  application/json
// @Param    data body request.ImageAssessRequest true "图像评估请求参数"
// @Success  200  {object} response.Response{data=response.ImageAssessResponse} "评估成功"
// @Router   /assess/image2Points [post]
func (imageAssessApi *ImageAssessApi) Image2Points(c *gin.Context) {
	var req request.ImageAssessRequest

	// 绑定请求参数
	if err := c.ShouldBindJSON(&req); err != nil {
		global.GVA_LOG.Error("参数绑定失败!", zap.Error(err))
		response.FailWithMessage("参数绑定失败: "+err.Error(), c)
		return
	}

	// 参数验证
	if req.AppraiseType == "" {
		response.FailWithMessage("评估类型不能为空", c)
		return
	}
	if req.ImageType == "" {
		response.FailWithMessage("图像类型不能为空", c)
		return
	}
	if req.URL == "" {
		response.FailWithMessage("图像URL不能为空", c)
		return
	}
	//if req.ImageKey == "" {
	//	response.FailWithMessage("图像标识不能为空", c)
	//	return
	//}

	// 调用服务层进行图像评估
	result, err := ImageAssessService.Image2Points(&req)
	if err != nil {
		global.GVA_LOG.Error("转换失败!", zap.Error(err))
		response.FailWithMessage("转换失败: "+err.Error(), c)
		return
	}

	// 返回成功响应
	response.OkWithDetailed(result, "转换成功", c)
}

// SubmitAssess 提交评估
// @Tags     Assess
// @Summary  提交评估 - 调用第三方API保存评估结果
// @Security ApiKeyAuth
// @accept   application/json
// @Produce  application/json
// @Param    data body request.SaveAssessResultRequest true "保存评估结果请求参数"
// @Success  200  {object} response.Response{data=map[string]interface{}} "保存成功"
// @Router   /assess/submitAssess [post]
func (imageAssessApi *ImageAssessApi) SubmitAssess(c *gin.Context) {
	var req request.SaveAssessResultRequest

	// 绑定请求参数
	if err := c.ShouldBindJSON(&req); err != nil {
		global.GVA_LOG.Error("参数绑定失败!", zap.Error(err))
		response.FailWithMessage("参数绑定失败: "+err.Error(), c)
		return
	}

	// 参数验证
	if req.AppraiseImages == "" {
		response.FailWithMessage("评估图像数据不能为空", c)
		return
	}
	if req.AppraiseTypeId == 0 {
		response.FailWithMessage("评估类型ID不能为空", c)
		return
	}
	if req.MbName == "" {
		response.FailWithMessage("姓名不能为空", c)
		return
	}
	if req.Birthday == "" {
		response.FailWithMessage("生日不能为空", c)
		return
	}
	if req.Kg == 0 {
		response.FailWithMessage("体重不能为空", c)
		return
	}
	if req.Cm == 0 {
		response.FailWithMessage("身高不能为空", c)
		return
	}

	// 调用服务层保存评估结果
	result, err := ImageAssessService.SubmitAssess(&req)
	if err != nil {
		global.GVA_LOG.Error("获取评估结果失败!", zap.Error(err))
		response.FailWithMessage("获取评估结果失败: "+err.Error(), c)
		return
	}

	// 返回成功响应
	response.OkWithDetailed(result, "获取成功", c)
}

// GetAssessRecordList 获取评估记录列表
// @Tags     AssessRecord
// @Summary  获取评估记录列表
// @Security ApiKeyAuth
// @accept   application/json
// @Produce  application/json
// @Param    data body request.AssessRecordSearch true "查询参数"
// @Success  200  {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router   /assess/getAssessRecordList [get]
// GetAssessRecordList 获取评估记录列表 - 按日期分组，通过健管师ID过滤
// @Tags     Assess
// @Summary  获取评估记录列表 - 按日期分组显示
// @Security ApiKeyAuth
// @accept   application/json
// @Produce  application/json
// @Param    data query request.AssessRecordSearch true "查询参数"
// @Success  200  {object} response.Response{data=response.PageResult} "获取成功"
// @Router   /assess/getAssessRecordList [get]
func (imageAssessApi *ImageAssessApi) GetAssessRecordList(c *gin.Context) {
	var pageInfo request.AssessRecordSearch

	// 绑定请求参数
	if err := c.ShouldBindQuery(&pageInfo); err != nil {
		global.GVA_LOG.Error("参数绑定失败!", zap.Error(err))
		response.FailWithMessage("参数绑定失败: "+err.Error(), c)
		return
	}

	// 获取健管师ID
	hmID := utils.GetHmID(c)

	// 调用服务层获取评估记录列表
	list, total, err := ImageAssessService.GetAssessRecordList(pageInfo, hmID)
	if err != nil {
		global.GVA_LOG.Error("获取评估记录列表失败!", zap.Error(err))
		response.FailWithMessage("获取评估记录列表失败: "+err.Error(), c)
		return
	}

	// 返回成功响应
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取评估记录列表成功", c)
}

// GetAssessRecord 获取评估记录详情
// @Tags     AssessRecord
// @Summary  获取评估记录详情
// @Security ApiKeyAuth
// @accept   application/json
// @Produce  application/json
// @Param    id path uint true "评估记录ID"
// @Success  200  {object} response.Response{data=object,msg=string} "获取成功"
// @Router   /assess/getAssessRecord [get]
func (imageAssessApi *ImageAssessApi) GetAssessRecord(c *gin.Context) {
	// 获取路径参数
	idStr := c.Query("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.FailWithMessage("无效的记录ID", c)
		return
	}

	// 调用服务层获取评估记录详情
	record, err := ImageAssessService.GetAssessRecord(uint(id))
	if err != nil {
		global.GVA_LOG.Error("获取评估记录详情失败!", zap.Error(err))
		response.FailWithMessage("获取评估记录详情失败: "+err.Error(), c)
		return
	}

	// 返回成功响应
	response.OkWithDetailed(record, "获取评估记录详情成功", c)
}
